﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        /// <summary>
        /// 缓存池
        /// </summary>
        private byte[] Buffer { get; set; }
        /// <summary>
        /// 缓存区大小
        /// </summary>
        public int BufferSize { get; set; }

        //Calculate the folder's total size
        private static Int64 CalculateFolderSize(string FolderName)
        {
            if (Directory.Exists(FolderName) == false)
            {
                throw new DirectoryNotFoundException("文件不存在");
            }
            DirectoryInfo rootDir = new DirectoryInfo(FolderName);
            //Get all subfolders
            DirectoryInfo[] childDirs = rootDir.GetDirectories();
            //Get all files of current folder
            FileInfo[] files = rootDir.GetFiles();
            Int64 totalSize = 0;
            //sum every file size
            foreach (FileInfo file in files)
            {
                totalSize += file.Length;

            }
            //sum every folder
            foreach (DirectoryInfo dir in childDirs)
            {
                totalSize += CalculateFolderSize(dir.FullName);
            }
            return totalSize;

        }

        private static Int64 AsynCalculateFolderSize(string FolderName)
        {
            if (Directory.Exists(FolderName) == false)
            {
                throw new DirectoryNotFoundException("文件不存在");
            }
            DirectoryInfo rootDir = new DirectoryInfo(FolderName);
            //Get all subfolders
            DirectoryInfo[] childDirs = rootDir.GetDirectories();
            //Get all files of current folder
            FileInfo[] files = rootDir.GetFiles();
            Int64 totalSize = 0;
            //sum every file size
            foreach (FileInfo file in files)
            {
                totalSize += file.Length;

            }
            //sum every folder
            foreach (DirectoryInfo dir in childDirs)
            {
                totalSize += AsynCalculateFolderSize(dir.FullName);
            }
            return totalSize;

        }

        /// <summary>
        /// 异步读取文件
        /// </summary>
        /// <param name="path"></param>
        public void AsynReadFile(string path)
        {
            Console.WriteLine("异步读取文件 begin");
            //执行Endread时报错，fs已经释放，注意在异步中不能使用释放需要的资源
            //using (FileStream fs = new FileStream(path, FileMode.Open))
            //{
            //    Buffer = new byte[BufferSize];
            //    fs.BeginRead(Buffer, 0, BufferSize, AsyncReadCallback, fs);
            //} 
            if (File.Exists(path))
            {
                FileStream fs = new FileStream(path, FileMode.Open);
                fs.BeginRead(Buffer, 0, BufferSize, AsyncReadCallback, fs);
            }
            else
            {
                Console.WriteLine("该文件不存在");
            }

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ar"></param>
        void AsyncReadCallback(IAsyncResult ar)
        {
            FileStream stream = ar.AsyncState as FileStream;
            if (stream != null)
            {
                Thread.Sleep(1000);
                //读取结束
                stream.EndRead(ar);
                stream.Close();

                string output = System.Text.Encoding.UTF8.GetString(this.Buffer);
                Console.WriteLine("读取的文件信息：{0}", output);
            }
        }

        static void Main(string[] args)
        {
            Int64 size;
            String FolderName;
            Console.WriteLine("Please input the name of folder (C:\\Windows):");
            FolderName = Console.ReadLine();
            size = CalculateFolderSize(FolderName);
            Console.WriteLine("\nThe size of folder {0} is {1}字节\n", FolderName, size);
            Console.ReadKey();
        }
    }
}
